package com.xsq.audio;

import android.content.ComponentName;
import android.content.Context;
import android.support.v4.media.MediaBrowserCompat;
import android.support.v4.media.MediaMetadataCompat;
import android.support.v4.media.session.MediaControllerCompat;
import android.support.v4.media.session.PlaybackStateCompat;

/**
 * 连接服务操作
 * Created by Shiquan.Xiao on 2023/3/28.
 */
public class AudioServiceConnection implements IServiceConnection{

    public static final PlaybackStateCompat EMPTY_PLAYBACK_STATE = new PlaybackStateCompat.Builder()
            .setState(PlaybackStateCompat.STATE_NONE, 0, 0f)
            .build();
    public static final MediaMetadataCompat NOTHING_PLAYING = new MediaMetadataCompat.Builder()
            .putString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID, "")
            .putLong(MediaMetadataCompat.METADATA_KEY_DURATION, 0)
            .build();
    private final MediaBrowserCompat mediaBrowser;
    private final MediaBrowserCompat.SubscriptionCallback subscriptionCallback;

    public AudioServiceConnection(Builder builder){
        mediaBrowser = builder.mediaBrowser;
        subscriptionCallback = builder.subscriptionCallback;

    }

    public static class Builder {

        private final Context context;
        private ComponentName serviceComponentName;
        private MediaBrowserCompat mediaBrowser;
        private MediaBrowserCompat.SubscriptionCallback subscriptionCallback;
        private IServiceConnectionListener connectionListener;
        private IAudioPlayingListener audioPlayingListener;

        public Builder(Context context) {
            this.context = context;
        }

        public Builder setConnectionListener(IServiceConnectionListener connectionListener) {
            this.connectionListener = connectionListener;
            return this;
        }

        public Builder setAudioPlayingListener(IAudioPlayingListener audioPlayingListener) {
            this.audioPlayingListener = audioPlayingListener;
            return this;
        }

        public Builder setSubscriptionCallback(MediaBrowserCompat.SubscriptionCallback subscriptionCallback) {
            this.subscriptionCallback = subscriptionCallback;
            return this;
        }

        public Builder setServiceComponentName(ComponentName serviceComponentName) {
            this.serviceComponentName = serviceComponentName;
            return this;
        }

        public AudioServiceConnection build() {
            mediaBrowser = new MediaBrowserCompat(
                    context,
                    serviceComponentName,
                    new ConnectionCallback(), null
            );

            return new AudioServiceConnection(this);
        }

        private class AudioMediaController extends MediaControllerCompat.Callback {

            @Override
            public void onPlaybackStateChanged(PlaybackStateCompat state) {
                if (state == null) {
                    audioPlayingListener.onPlayingState(EMPTY_PLAYBACK_STATE);
                } else {
                    audioPlayingListener.onPlayingState(state);
                }

            }

            @Override
            public void onMetadataChanged(MediaMetadataCompat metadata) {
                if (audioPlayingListener != null) {
                    if (metadata == null || metadata.getString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID) == null) {
                        audioPlayingListener.onPlayingData(NOTHING_PLAYING);
                    } else {
                        audioPlayingListener.onPlayingData(metadata);
                    }
                }
            }

        }

        private class ConnectionCallback extends MediaBrowserCompat.ConnectionCallback {
            @Override
            public void onConnected() {
                MediaControllerCompat mediaController = new MediaControllerCompat(context, mediaBrowser.getSessionToken());
                mediaController.registerCallback(new AudioMediaController());

                connectionListener.onConnected(mediaController);
            }

            @Override
            public void onConnectionFailed() {
                connectionListener.onFailure();
            }
        };

    }

    @Override
    public void connect() {
        mediaBrowser.connect();
    }

    @Override
    public void disConnect() {
        if (mediaBrowser.isConnected()) {
            mediaBrowser.disconnect();
        }
    }

    @Override
    public boolean isConnected() {
        return mediaBrowser.isConnected();
    }

    @Override
    public void subscribeBrowserService(String rootId) {
        mediaBrowser.subscribe(rootId, subscriptionCallback);
    }

    @Override
    public void unsubscribeBrowserService(String rootId) {
        mediaBrowser.unsubscribe(rootId, subscriptionCallback);
    }

}
